<?php

namespace App\Http\Controllers\Commit;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Commit\DictDatarRepository;
use App\Rules\DictDataValidate;
use Illuminate\Http\Request;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class DictDatarController extends BaseController
{
    public function __construct(DictDatarRepository $DictDatarRepository)
    {
        $this->repoist = $DictDatarRepository;
    }

    public function index(Request $Request, $type)
    {
        //字段的获取
        return $this->repoist->oneSel($type);
    }

//取一些单个设置的值给前台
    public function singleTypeValue(Request $request)
    {
        $type = $request->get('type');
        if (empty($type)) {
            return $this->returnMsg(200, '查询成功', []);
        }
        $value = $this->repoist->singleTypeValue($type);
        return $this->returnMsg(200, '查询成功', $value);
    }

// 以下方法是自己还原的字典方法
    public function listIt(Request $request)
    {
        //字段的获取
        $where = [];
        if ($request->has('dict_type') && !empty($request->get('dict_type'))) {
            $where['where']['dict_type'] = $request->get('dict_type');
        }
        if ($request->has('status') && !empty($request->get('status'))) {
            $where['where']['status'] = $request->get('status');
        }
        if ($request->has('dict_label') && !empty($request->get('dict_label'))) {
            $where['where']['dict_label'] = $request->get('dict_label');
        }
        $where['order'] = ['orderby' => 'dict_code', 'orderdir' => 'desc'];
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $where['limit']['number'] = $request->get('pageSize');
        return $this->repoist->unifiedIndex($where);
    }

    public function typeOptions(Request $Request)
    {
        $returnData = $this->repoist->typeOptions();
        return $this->returnMsg(200, '查询成功', $returnData);
    }

    public function edit(Request $request)
    {
        $Validator = new DictDataValidate();
        if (!$Validator->check($request->all())) {
            return $this->returnMsg(1, $Validator->getError());
        }

        if ($request->has('dict_code') && $request->get('dict_code') > 0) {
            return $this->repoist->unifiedEdit($request);
        } else {
            return $this->repoist->unifiedAdd($request);
        }
    }

    public function add(Request $request)
    {
        return $this->edit($request);
    }

    public function changeStatus(Request $request)
    {
        $id = $request->get("id");
        $status = (int)$request->get('status');
        return $this->repoist->unifiedUpdateStatus($id, $status);
    }
}
